<template>
	<view>
		<!-- 已借书籍数量统计 -->
		<view class="book_div">
			<view class="count">
				<text>
					共借阅：
				</text>
				<text>
					{{ordersCount}}
				</text>

			</view>
			<view class="count">
				<text>
					待归还：
				</text>
				<text>
					{{notReturn}}
				</text>
			</view>
		</view>
		<!-- 已借书籍列表 -->
		<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" :options="formData" :collection="collection" @load="load"
			where="uid==$env.uid" :field="field" orderby="date_of_borrowing, returned">
			<uni-list>
				<uni-list-item direction="column" v-for="item in data" :key="item._id"
					:to="'/pages/library/detail/detail?book_id='+item.book_id[0].book_id">
					<template v-slot:body>
						<view style="display: flex; justify-content: space-between;">
							<view>
								<text class="text-grey">
									{{item.book_id[0].title}}
								</text>
								<view>
									<uni-dateformat class="text-grey text-sm" :date="item.date_of_borrowing"
										format="yyyy年MM月dd日"></uni-dateformat>
								</view>
							</view>

							<view class="book_return" v-if="!item.returned">
								<text>借阅过期: </text>
								<uni-dateformat style="color: #008000;" :date="item.date_of_return"
									threshold="[0,25920000000]"></uni-dateformat>
							</view>
						</view>
					</template>
				</uni-list-item>
			</uni-list>
			<uni-load-more v-if="loading || options.status === 'noMore' " :status="options.status" />
		</unicloud-db>
	</view>
</template>

<script>
	const app = getApp();
	const db = uniCloud.database();
	export default {
		data() {
			return {
				collection: 'library-orders, library-books',
				field: "_id, book_id{title, book_id,book_thumb}, returned, date_of_return, date_of_borrowing, uid",
				ordersCount: 0,
				notReturn: 0,
				formData: {
					status: 'loading', // 加载状态
				},
			}
		},
		methods: {
			onShow() {
				if (!app.globalData.hasLogin) {
					uni.showModal({
						title: "请先登录",
						mask: true,
						success: (res) => {
							if (res.confirm) {
								uni.switchTab({
									url: "/pages/profile/profile"
								})
							} else {
								uni.navigateBack({
									delta: 1
								})
							}
						}
					});
				}
				db.collection('library-orders').where({
					uid: app.globalData.userInfo._id
				}).count().then(res => {
					this.ordersCount = res.result.total
				})
				db.collection('library-orders').where({
					uid: app.globalData.userInfo._id,
					returned: false
				}).count().then(res => {
					this.notReturn = res.result.total
				})
			},

			/**
			 * 下拉刷新回调函数
			 */
			onPullDownRefresh() {
				this.tipShow = true
				this.formData.status = 'more'
				this.$refs.udb.loadData({
					clear: true
				}, () => {
					this.tipShow = false
					uni.stopPullDownRefresh()
				})
			},
			/**
			 * 上拉加载回调函数
			 */
			onReachBottom() {
				this.$refs.udb.loadMore()
			},

			load(data, ended) {
				if (ended) {
					this.formData.status = 'noMore'
				}
			},

		}
	}
</script>

<style>
	.book_div {
		display: flex;
		background-color: #FFFFFF;
		border-bottom: #000000;
	}

	.book_div .count {
		flex-basis: 50%;
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.book_return {
		display: flex;
		flex-direction: column;
		width: 200rpx;
		justify-content: end;
		align-items: flex-end;
	}
</style>
